package com.sinya.shoppingmall.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;

import com.sinya.shoppingmall.R;


/**
 * 动态的控制宽高比例
 * ①已知准确的宽度值，根据宽高比例，计算新的高度值
 * ②已知准确的高度值，根据宽高比例，计算新的宽度值
 * <p/>
 * author : Sinya Koizumi
 * createDate ：2015/8/19 14:38
 * editor：
 * updateDate：2015/8/19 14:38
 */
public class RatioLayout extends FrameLayout {

    public static final int RELATIVE_WIDTH = 0;
    public static final int RELATIVE_HEIGHT = 1;

    /**
     * 宽高比
     */
    private float ratio;

    /**
     * 使用什么作为参考计算标准
     */
    private int relative = RELATIVE_WIDTH;

    public void setRatio(float ratio) {
        this.ratio = ratio;
    }

    public void setRelative(int relative) {
        if (relative > 1 || relative < 0) {
            return;
        }
        this.relative = relative;
    }

    public RatioLayout(Context context) {
        super(context);
    }

    public RatioLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyRatioLayout);
        ratio = typedArray.getFloat(R.styleable.MyRatioLayout_ratio, 0f);
        relative = typedArray.getInt(R.styleable.MyRatioLayout_relative, RELATIVE_WIDTH);

        typedArray.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        // 获取宽度的大小和模式
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        // 获取高度的大小和模式
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        //①已知宽度的准确值，通过宽高比例，计算出高度值
        if (widthMode == MeasureSpec.EXACTLY && ratio != 0f && relative == RELATIVE_WIDTH) {
            int width = widthSize - getPaddingLeft() - getPaddingRight();
            int height = (int) (width / ratio + 0.5f);

            int childWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

            // * Ask all of the children of this view to measure themselves, taking into
           // * account both the MeasureSpec requirements for this view and its padding.
            measureChildren(childWidth, childHeight);

            int parentWidth = widthSize;
            int parentHeight = height + getPaddingTop() + getPaddingBottom();
            setMeasuredDimension(parentWidth, parentHeight);

            //②已知高度的准确值，计算宽高比例，计算出宽度值
        } else if (heightMode == MeasureSpec.EXACTLY && ratio != 0f && relative == RELATIVE_HEIGHT) {
            int height = heightSize - getPaddingTop() - getPaddingBottom();
            int width = (int) (height * ratio + 0.5f);

            int childWidth = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            int childHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

            measureChildren(childWidth, childHeight);

            int parentWidth = width + getPaddingLeft() + getPaddingRight();
            int parentHeight = heightSize;
            setMeasuredDimension(parentWidth, parentHeight);

        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

}
